package utils;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;

/**
 * Created by wuzhao on 2016/12/12 0012.
 */
public class ThreadUtils {

    private static ThreadPoolTaskExecutor poolTaskExecutor;

    static {
        poolTaskExecutor = new ThreadPoolTaskExecutor();
        //线程池所使用的缓冲队列
        poolTaskExecutor.setQueueCapacity(200);
        //线程池维护线程的最少数量
        poolTaskExecutor.setCorePoolSize(5);
        //线程池维护线程的最大数量
        poolTaskExecutor.setMaxPoolSize(1000);
        //线程池维护线程所允许的空闲时间
        poolTaskExecutor.setKeepAliveSeconds(30000);
        poolTaskExecutor.initialize();
    }

    public static void executeThread(Thread thread){
        if (thread == null)
            return;
        poolTaskExecutor.execute(thread);
    }

    public static <T> Future<T> submit(Callable<T> task){
        if (task == null)
            return null;
        return poolTaskExecutor.submit(task);
    }

    public static ThreadPoolTaskExecutor getPoolTaskExecutor() {
        return poolTaskExecutor;
    }

    public static void main(String[] args) {

    }

}
